package chat.aylan.top.client;

import chat.aylan.top.CloseUtil;

import java.io.*;
import java.net.Socket;

public class SenderTread implements Runnable {

    private BufferedReader reader;
    private DataOutputStream writer;

    public SenderTread(Socket socket){
        try {
            reader = new BufferedReader(new InputStreamReader(System.in));
            writer = new DataOutputStream(socket.getOutputStream());
        } catch (IOException e) {
            die();
        }
    }


    private void readInput(){
        String str;
        try {
            str = reader.readLine();
            while (null!=str&& !str.equals("")){
                sendMsg(str);
                str = reader.readLine();
            }
        } catch (IOException e) {
            die();
        }
    }


    private void sendMsg(String msg){
        try {
            writer.writeUTF(msg);
            writer.flush();
        } catch (IOException e) {
            die();
        }
    }

    @Override
    public void run() {
        readInput();
    }

    private void die(){
        CloseUtil.closeAll(writer,reader);
    }
}
